<?php
	require_once("includes/functions.php");
	require_once("includes/session_functions.php");
	require_once("includes/connection.php");

class Login extends CI_Controller{
	public function index(){
			session_start();
		if (isset($_POST['submit'])) {
			$username = trim($_POST['username']);
			$password = trim($_POST['password']);
			$hashed_password = sha1($password);
			
			$query = "SELECT id, username, type ";
			$query .= "FROM user ";
			$query .= "WHERE username = '{$username}' ";
			$query .= "AND password = '{$hashed_password}' ";
			$query .= "LIMIT 1";
			$result_set = mysql_query($query);

			if (mysql_num_rows($result_set) == 1) {
				
				$found_user = mysql_fetch_array($result_set);
				$_SESSION['user_id'] = $found_user['id'];
				$_SESSION['username'] = $found_user['username'];
				$_SESSION['type'] = $found_user['type'];
				if($found_user['type'] == 'attendee'){
					redirect_to("attendee_list/view_dashboard/{$found_user['id']}");
				}
				else if($found_user['type'] == 'presenter'){
					redirect_to("presenter_list/view_dashboard/{$found_user['id']}");
				}
			} else {
				redirect_to("pages/view/home");
			}
		}
	}
}
?>